package com.jxau.service.impl;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jxau.entity.Article;
import com.jxau.entity.Comment;
import com.jxau.entity.Temp;
import com.jxau.entity.view.IndexArticles;
import com.jxau.mapper.ArticleMapper;
import com.jxau.service.ArticleService;
import com.jxau.util.Constant;
import com.jxau.util.HtmlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author lts
 * @create 2022-05-09 21:29
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public ArrayList<IndexArticles> queryAllArticles() {
        ArrayList<IndexArticles> articles = articleMapper.queryAllArticles();
        for (IndexArticles article : articles) {
            article.setContent(HtmlUtil.parseHtml(article.getContent(), 60));
        }
        return articles;
    }


    @Override
    public ArrayList<IndexArticles> queryAllArticlesWithUid(String uid, int nowPage) {
        PageHelper.startPage(nowPage, Constant.ARTICLE_PAGE_SIZE);
        ArrayList<IndexArticles> articles = articleMapper.queryAllArticlesWithUid(uid);
        for (IndexArticles article : articles) {
            article.setContent(HtmlUtil.parseHtml(article.getContent(), 60));
        }
        return articles;
    }

    @Override
    public ArrayList<IndexArticles> queryFollowUsersArticles(String uid, int nowPage) {
        PageHelper.startPage(nowPage, Constant.ARTICLE_PAGE_SIZE);
        ArrayList<IndexArticles> articles = articleMapper.queryFollowUsersArticles(uid);
        for (IndexArticles article : articles) {
            article.setContent(HtmlUtil.parseHtml(article.getContent(), 60));
        }
        return articles;
    }

    @Override
    public void addArticle(Article article) {
        article.setView(0);
        article.setDate(new Date());
        articleMapper.addArticle(article);
    }

    @Override
    public Article queryOneArticleWithUid(int articleId, String uid) {
        return articleMapper.queryOneArticleWithUid(articleId, uid);
    }


    @Override
    public ArrayList<Article> queryArticlesByAuthorId(String authorId, int nowPage) {
        PageHelper.startPage(nowPage, Constant.MY_ARTICLE_PAGE_SIZE);
        ArrayList<Article> articles = articleMapper.queryArticlesByAuthorId(authorId);
        for (Article article : articles) {
            article.setContent(HtmlUtil.parseHtml(article.getContent(), 60));
        }
        return articles;
    }

    @Override
    public ArrayList<IndexArticles> findArticleByStrWithLogin(String str, String uid, int nowPage) {
        PageHelper.startPage(nowPage, Constant.ARTICLE_PAGE_SIZE);
        ArrayList<IndexArticles> articles = articleMapper.findArticleByStrWithLogin(str, uid);
        for (IndexArticles article : articles) {
            article.setContent(HtmlUtil.parseHtml(article.getContent(), 60));
        }
        return articles;
    }

    @Override
    public ArrayList<Article> queryMyCollection(String uid, int nowPage) {
        PageHelper.startPage(nowPage, Constant.MY_ARTICLE_PAGE_SIZE);
        ArrayList<Article> articles = articleMapper.queryMyCollection(uid);
        for (Article article : articles) {
            article.setContent(HtmlUtil.parseHtml(article.getContent(), 60));
        }
        return articles;
    }

    @Override
    public void viewPlus(int articleId, HttpSession session) {
        Object user = session.getAttribute("user");
        if (user != null) {
            articleMapper.viewPlus(articleId);
        }
    }
    //-------------------------------------------------------------------


    public PageInfo<Temp> getPagesData() {
        List<Temp> articles = articleMapper.getAllArticles();
        PageInfo<Temp> pageInfo = new PageInfo<>(articles);
        return pageInfo;
    }

    @Override
    public List<Temp> getArticleById(int id) {
        return articleMapper.getArticleById(id);
    }

    @Override
//    @Transactional
    public int deleteArticle(int id) {
//        List<Comment> list = commentMapper.selectCommentByArticleId(id);
//        // 删除帖子下面评论的相关点赞记录
//        for (Comment comment : list) {
//            commentMapper.deleteLikeByCommentId(comment.getId());
//        }
//        // 删除帖子下评论
//        commentMapper.deleteCommentByArticleId(id);
//        // 删除帖子相关点赞记录
//        articleMapper.deleteLikeByArticleId(id);
//        // 删除帖子相关收藏记录
//        articleMapper.deleteCollectByArticleId(id);
//        // 删除帖子
        return articleMapper.deleteArticle(id);
    }

    @Override
    public void makeTop(int articleId) {
        articleMapper.updateTop(articleId, 1);
    }

    @Override
    public void cancelTop(int articleId) {
        articleMapper.updateTop(articleId, 0);
    }


    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @Override
    public List<Temp> getPages(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Temp> articles = articleMapper.getAllArticles();
        return articles;
    }
}
